package controller;

//import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSON;
import dao.impl.GoodDaoImpl;
import po.Good;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class GetGoodsList extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String goodname = req.getParameter("goodname");
        System.out.println(goodname);
        GoodDaoImpl goodDao = new GoodDaoImpl();
        StringBuilder sql = new StringBuilder("select * from good where 1 = 1 ");
        if (goodname != null) {//查询时用
            sql.append(" and good_name like '%").append(goodname).append("%'");
        }
//        sql.append("ORDER BY `good_id` ASC");
//        System.out.println(sql);
        List<Good> goods = goodDao.selectGood(String.valueOf(sql));

        String goodsJson = JSON.toJSONString(goods);

        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");


        PrintWriter writer = resp.getWriter();
//        writer.write(JSON.toJSONString(goods.size()));
        writer.write(goodsJson);
        writer.flush();
        writer.close();
    }
}

